﻿using Microsoft.Win32;
using SQLDeploy.Data.Files;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SQLDeploy.Builder.Controls.Environments
{
    /// <summary>
    /// Interaction logic for Editor.xaml
    /// </summary>
    public partial class Editor : UserControl
    {
        public Editor()
        {
            Init(null);
        }

        public Editor(string environmentFile)
        {
            Init(environmentFile);
        }

        private void Init(string environmentFile)
        {
            InitializeComponent();
            //SQLDeployAnything.Data.Files.Connections ConnectionsFile;
            SQLDeploy.Data.Files.Connections connectionsFile;
            if (string.IsNullOrEmpty(environmentFile))
            {
                connectionsFile = new SQLDeploy.Data.Files.Connections();
            }
            else
            {
                connectionsFile = SQLDeploy.Data.Files.Connections.LoadFromFile(environmentFile);
            }
            this.DataContext = connectionsFile;
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.Title = "Select Connection File to Open";
            ofd.Filter = "Xml Files|(*.xml)|All Files|(*.*)";
            ofd.Multiselect = false;
            ofd.DefaultExt = ".xml";
            Nullable<bool> ok = ofd.ShowDialog();
            if (ok == true)
            {
                string file = ofd.FileName;
                this.txt_ConnectionFile.Text = file;
            }
        }


        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            AddDialog dialog = new AddDialog((SQLDeploy.Data.Files.Connections)this.DataContext);
            dialog.ShowDialog();
            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                ((SQLDeploy.Data.Files.Connections)this.DataContext).Environments.Add((Data.Environments.Environment)dialog.DataContext);
        }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            string selectedName = (string)this.cmb_SelectedEnvironment.SelectedValue;
            if (MessageBox.Show("Are you sure you want to delete the environment '" + selectedName +"'?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning,MessageBoxResult.No)
                == MessageBoxResult.Yes)
            {
                ((SQLDeploy.Data.Files.Connections)DataContext).Environments.Remove(
                    (Data.Environments.Environment)cmb_SelectedEnvironment.SelectedItem);
            }
        }

        private void Clone_Click(object sender, RoutedEventArgs e)
        {
            AddDialog dialog = new AddDialog((SQLDeploy.Data.Files.Connections)this.DataContext, 
                (Data.Environments.Environment)this.cmb_SelectedEnvironment.SelectedItem);
            dialog.ShowDialog();
            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                ((SQLDeploy.Data.Files.Connections)this.DataContext).Environments.Add(
                    (Data.Environments.Environment)dialog.DataContext);
        }


        

    }
}
